package com.hongkuncheng.cms.controller.admin;

import java.io.File;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Controller;
import com.hongkuncheng.cms.helper.PageHelper;
import com.hongkuncheng.cms.model.Watermark;
import com.hongkuncheng.cms.controller.BaseController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 图片水印设置
 *
 * @author 洪坤成
 */
@Controller
@Transactional
@RequestMapping("admin/watermark")
public class AwatermarkController extends BaseController {

    // 图片水印设置
    @RequestMapping
    public String index() {
        Watermark watermark = factorySession.get(Watermark.class, 1);
        request.setAttribute("watermark", watermark);
        File imagefile_temp = new File(applicationDirectory + "/resource/image/watermark_tmp.png");
        if (imagefile_temp.exists()) imagefile_temp.delete();
        return view();
    }

    // 图片水印设置更新
    @RequestMapping("update")
    @ResponseBody
    public void update() {
        checkAdminPermission("yunwei_systemsetup_watermark");
        Watermark watermark = factorySession.get(Watermark.class, 1);
        watermark.setEndble(request.getParameter("endble") != null);
        watermark.setPosition(Integer.parseInt(request.getParameter("position")));
        watermark.setMarginX(Integer.parseInt(request.getParameter("margin_x")));
        watermark.setMarginY(Integer.parseInt(request.getParameter("margin_y")));
        watermark.setTransparence(Double.parseDouble(request.getParameter("transparence")));
        request.getSession().setAttribute("watermark", watermark);
        // 更新水印图片
        File imagefile_temp = new File(applicationDirectory + "/resource/image/watermark_tmp.png");
        if (imagefile_temp.exists()) {
            File imagefile = new File(applicationDirectory + "/resource/image/watermark.png");
            imagefile.delete();
            imagefile_temp.renameTo(imagefile);
        }
        PageHelper.alertAndRedirect("更新成功", "./");
    }

}
